{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Anaconda 默认不包含 docx工具包，需要执行以下脚本进行安装\n",
    "# 如果本地没有安装工具包，请去掉“#”，再执行\n",
    "# !pip install -i https://mirrors.aliyun.com/pypi/simple/ python-docx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1、导入包并创建一个 Word 文档对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import docx\n",
    "# 从docx 工具包中导入要使用的对象\n",
    "from docx import Document\n",
    "\n",
    "# Document 括号中不包含文件路径就是在内存中创建 Word 对象\n",
    "document = Document()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2、增加标题和段落"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加顶级标题（level 的范围 从 0 到 9），level值越小，标题字越大，所以 0 就是顶级标题\n",
    "# 实际上标题也是段落 paragraph\n",
    "document.add_heading('Word 对象说明', level=0)\n",
    "\n",
    "# 创建一个段落对象并赋值给 p1\n",
    "p1 = document.add_paragraph('Document 对象包含多个 Paragraph 对象，')\n",
    "# 查看一下\n",
    "print(\"新增段落后，该段落有 %s 个文字块\" % len(p1.runs))\n",
    "\n",
    "# 在段落 p1 后增加文字块 \n",
    "p1.add_run('Paragraph 对象包含多个 Run 对象')\n",
    "print(\"增加一个文字块后，段落有 %s 个文字块\" % len(p1.runs))\n",
    "\n",
    "# 保存文件名\n",
    "save_file = r'.\\files\\Word_对象说明.docx'\n",
    "# 保存内存中的document对象到文件\n",
    "document.save(save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3、增加无序列表段落"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加一级标题（level=1）\n",
    "document.add_heading('无序列表', level=1)\n",
    "\n",
    "# 添加 3 个无序列表样式段落（List Bullet）\n",
    "# style 会在 Word样式 这个课程中详细讲解\n",
    "document.add_paragraph(\n",
    "    'Document 对象表示整个文档', \n",
    "    style='List Bullet'\n",
    ")\n",
    "document.add_paragraph(\n",
    "    'Paragraph 对象表示段落（每一次回车会产生新段落）', \n",
    "    style='List Bullet'\n",
    ")\n",
    "document.add_paragraph(\n",
    "    'Run 对象表示相同样式的文本延续', \n",
    "    style='List Bullet'\n",
    ")\n",
    "# 保存内存中的document对象到文件\n",
    "document.save(save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4、增加有序列表段落"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加一级标题（level=1）\n",
    "document.add_heading('有序列表', level=1)\n",
    "\n",
    "# 添加 3 个有序列表样式段落（List Number）\n",
    "document.add_paragraph(\n",
    "    'Document 对象表示整个文档', style='List Number'\n",
    ")\n",
    "document.add_paragraph(\n",
    "    'Paragraph 对象表示段落（每一次回车会产生新段落）', \n",
    "    style='List Number'\n",
    ")\n",
    "document.add_paragraph(\n",
    "    'Run 对象表示相同样式的文本延续', style='List Number'\n",
    ")\n",
    "# 保存内存中的document对象到文件\n",
    "document.save(save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 利用代码查看文档结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入读取docx文档结构函数\n",
    "from docx_ext import read_docx\n",
    "read_docx(save_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思考题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、尝试用段落Paragraph和文字块Run完成一个简单的Word文档\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
